﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BasicComponents.com
{
    /// <summary>
    /// Interaction logic for LabelTextBoxBtn.xaml
    /// </summary>
    public partial class LabelTextBoxBtn : UserControl, INotifyPropertyChanged
    {
        String text = "";
        //该参数保存输入框监测到的输入事件
        KeyEventArgs keyEventArgs = null;
        private Boolean isAlwaysShowBtn = true;
        public KeyEventArgs KeyEventArgs { get { return keyEventArgs; } set { keyEventArgs = value; } }
        public LabelTextBoxBtn()
        {
            InitializeComponent();
            this.TBox_text.DataContext = this;
            if (IsAlwaysShowBtn) this.Btn.Visibility = Visibility.Visible; else this.Btn.Visibility = Visibility.Collapsed;
        }
        //3、注册readOnly
        public Boolean IsReadOnly
        {
            get { return this.TBox_text.IsReadOnly; }
            set
            {
                this.TBox_text.IsReadOnly = value;
                if (((Boolean)value))
                {
                    this.TBox_text.Background = Brushes.LightGray;
                }
                else
                {
                    this.TBox_text.Background = Brushes.White;
                }
            }
        }
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(Boolean), typeof(LabelTextBoxBtn), new PropertyMetadata(false, (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.TBox_text.IsReadOnly = (Boolean)e.NewValue;
                if (((Boolean)e.NewValue))
                {
                    sender.TBox_text.Background = Brushes.LightGray;
                }
                else
                {
                    sender.TBox_text.Background = Brushes.White;
                }
            }));
        //3、注册readOnly
        public new Boolean IsEnabled
        {
            get { return this.TBox_text.IsEnabled; }
            set
            {
                this.TBox_text.IsEnabled = value;
                this.Btn.IsEnabled = value;
                if (!((Boolean)value))
                {
                    this.TBox_text.Background = Brushes.LightGray;
                }
                else
                {
                    this.TBox_text.Background = Brushes.White;
                }
            }
        }
        public new static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.Register("IsEnabled", typeof(Boolean), typeof(LabelTextBoxBtn), new PropertyMetadata(true, (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.TBox_text.IsEnabled = (Boolean)e.NewValue;
                sender.Btn.IsEnabled = (Boolean)e.NewValue;
                if (!((Boolean)e.NewValue))
                {
                    sender.TBox_text.Background = Brushes.LightGray;
                }
                else
                {
                    sender.TBox_text.Background = Brushes.White;
                }
            }));

        //3、注册readOnly
        public Boolean IsAlwaysShowBtn
        {
            get { return this.isAlwaysShowBtn; }
            set
            {
                this.isAlwaysShowBtn = value;
                if ((Boolean)value)
                {
                    if (this.Btn.Visibility.Equals(Visibility.Collapsed))
                    {
                        this.TBox_text.Width -= this.Btn.Width;
                    }
                    this.Btn.Visibility = Visibility.Visible;
                }
                else
                {
                    if (this.Btn.Visibility.Equals(Visibility.Visible))
                    {
                        this.TBox_text.Width += this.Btn.Width;
                    }
                    this.Btn.Visibility = Visibility.Collapsed;
                }
            }
        }
        public static readonly DependencyProperty IsAlwaysShowBtnProperty =
            DependencyProperty.Register("IsAlwaysShowBtn", typeof(Boolean), typeof(LabelTextBoxBtn), new PropertyMetadata(true, (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.IsAlwaysShowBtn = (Boolean)e.NewValue;
                if ((Boolean)e.NewValue)
                {
                    if (sender.Btn.Visibility.Equals(Visibility.Collapsed))
                    {
                        sender.TBox_text.Width -= sender.Btn.Width;
                    }
                    sender.Btn.Visibility = Visibility.Visible;
                }
                else
                {
                    if (sender.Btn.Visibility.Equals(Visibility.Visible))
                    {
                        sender.TBox_text.Width += sender.Btn.Width;
                    }
                    sender.Btn.Visibility = Visibility.Collapsed;
                }
            }));

        //3、注册文本框的宽度Width wxt20191117 为了网报审批流程条件添加
        public double TextBoxWidth
        {
            get { return (Double)GetValue(TextBoxWidthProperty); }
            set { SetValue(TextBoxWidthProperty, value); }
        }
        public static readonly DependencyProperty TextBoxWidthProperty =
            DependencyProperty.Register("TextBoxWidth", typeof(Double), typeof(LabelTextBoxBtn), new PropertyMetadata(defaultValue: 0.00, propertyChangedCallback: null, coerceValueCallback: coerceValueCallback));
        private static object coerceValueCallback(DependencyObject d, object baseValue)
        {
            if (baseValue != null)
            {
                var control = d as LabelTextBoxBtn;
                control.TBox_text.Width = Double.Parse(baseValue.ToString());
                return double.Parse(baseValue.ToString());
            }
            return double.Parse(baseValue.ToString());
        }
        //3、注册MinWidth
        //public new double MinWidth
        //{
        //    get { return (Double)GetValue(MinWidthProperty); }
        //    set { SetValue(MinWidthProperty, value); }
        //    //get { return this.grid_main.Width; }
        //    //set { this.grid_main.Width= value; }
        //}
        //public new static readonly DependencyProperty MinWidthProperty =
        //    DependencyProperty.Register("MinWidth", typeof(Double), typeof(LabelTextBoxBtn), new FrameworkPropertyMetadata(0.00, (s, e) =>
        //    {
        //        try
        //        {
        //            LabelTextBoxBtn sender = s as LabelTextBoxBtn;
        //            sender.grid_main.MinWidth = (double)e.NewValue;
        //        }
        //        catch (Exception e1)
        //        {
        //            MessageBox.Show(e1.Message);
        //        }
        //    }));

        //3、注册IsEnable
        public Boolean IsBtnEnabled
        {
            get { return this.Btn.IsEnabled; }
            set { this.Btn.IsEnabled = value; }

        }
        public static readonly DependencyProperty IsBtnEnabledProperty =
            DependencyProperty.Register("IsBtnEnabled", typeof(Boolean), typeof(LabelTextBoxBtn), new FrameworkPropertyMetadata(true, (s, e) =>
            {
                try
                {
                    LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                    sender.Btn.IsEnabled = (Boolean)e.NewValue;
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }
            }));

        //7、注册Label字体大小
        public int FontSize_Title
        {
            get { return (int)GetValue(FontSize_TitleProperty); }
            set { SetValue(FontSize_TitleProperty, value); }
        }
        public static readonly DependencyProperty FontSize_TitleProperty =
            DependencyProperty.Register("FontSize_Title", typeof(int), typeof(LabelTextBoxBtn), new PropertyMetadata(12, (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.Label_title.FontSize = (int)e.NewValue;
            }));
        //8、注册Label字体FontWeight
        public FontWeight FontWeight_Title
        {
            get { return (FontWeight)GetValue(FontWeight_TitleProperty); }
            set { SetValue(FontWeight_TitleProperty, value); }
        }
        public static readonly DependencyProperty FontWeight_TitleProperty =
            DependencyProperty.Register("FontWeight_Title", typeof(FontWeight), typeof(LabelTextBoxBtn), new PropertyMetadata(FontWeights.Normal, (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.Label_title.FontWeight = (FontWeight)e.NewValue;
            }));
        //1、标题
        public String Title
        {
            get { return this.Label_title.Content.ToString(); }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(String), typeof(LabelTextBoxBtn), new PropertyMetadata("", (s, e) =>
            {
                LabelTextBoxBtn sender = s as LabelTextBoxBtn;
                sender.Label_title.Content = e.NewValue.ToString();
            }));
        //2、内容块内容信息
        public String Text
        {
            get { return this.text; }
            set
            {
                text = value;
                SetValue(TextProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                }
            }
        }
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(LabelTextBoxBtn), new PropertyMetadata("", (s, e) =>
        {
            LabelTextBoxBtn sender = s as LabelTextBoxBtn;
            sender.Text = e.NewValue.ToString();
        }));

        //4、注册text宽度
        //public  Double TextBoxWidth
        //{
        //    get { return (Double)GetValue(TextBoxWidthProperty); }
        //    set { SetValue(TextBoxWidthProperty, value); }
        //}
        //public static readonly  DependencyProperty TextBoxWidthProperty =
        //    DependencyProperty.Register("TextBoxWidth", typeof(Double), typeof(LabelTextBoxBtn), new PropertyMetadata(120, (s, e) =>
        //    {
        //        LabelTextBoxBtn sender = s as LabelTextBoxBtn;
        //        sender.TBox_text.Width = (Double)e.NewValue;
        //    }));

        //按钮
        public Double BtnWidth
        {
            get { return (Double)GetValue(BtnWidthProperty); }
            set { SetValue(BtnWidthProperty, value); }
        }
        public static readonly DependencyProperty BtnWidthProperty = DependencyProperty.Register("BtnWidth", typeof(Double), typeof(LabelTextBoxBtn), new PropertyMetadata(23.00, (s, e) =>
         {
             LabelTextBoxBtn sender = s as LabelTextBoxBtn;
             sender.Btn.Width = Double.Parse(e.NewValue.ToString());
         }));

        //、整体框高度
        public new Double Height
        {
            get { return (Double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }
        public static readonly new DependencyProperty HeightProperty = DependencyProperty.Register("Height", typeof(Double), typeof(LabelTextBoxBtn), new PropertyMetadata(23.00, (s, e) =>
         {
             LabelTextBoxBtn sender = s as LabelTextBoxBtn;
             sender.TBox_text.Height = Double.Parse(e.NewValue.ToString());
             sender.Label_title.Height = Double.Parse(e.NewValue.ToString());
         }));

        //、按钮的高度
        public Double BtnHeight
        {
            get { return (Double)GetValue(BtnHeightProperty); }
            set { SetValue(BtnHeightProperty, value); }
        }
        public static readonly DependencyProperty BtnHeightProperty = DependencyProperty.Register("BtnHeight", typeof(Double), typeof(LabelTextBoxBtn), new PropertyMetadata(23.00, (s, e) =>
       {
           LabelTextBoxBtn sender = s as LabelTextBoxBtn;
           sender.Btn.Height = Double.Parse(e.NewValue.ToString());
       }));

        //、开放textbox wxt 201911
        public TextBox TextBox
        {
            get { return this.TextBox; } 
        }
        /*
        public static readonly DependencyProperty TextBoxProperty = DependencyProperty.Register("TextBox", typeof(TextBox), typeof(LabelTextBoxBtn), new PropertyMetadata(23.00, (s, e) =>
        {
            LabelTextBoxBtn sender = s as LabelTextBoxBtn;
            sender.Btn.Height = Double.Parse(e.NewValue.ToString());
        }));
        */

        //、按钮的高度
        public new Brush BorderBrush
        {
            get { return (Brush)GetValue(BorderBrushProperty); }
            set { SetValue(BorderBrushProperty, value); }
        }
        public static readonly new DependencyProperty BorderBrushProperty = DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(LabelTextBoxBtn), new PropertyMetadata(Brushes.Gray, (s, e) =>
        {
            LabelTextBoxBtn sender = s as LabelTextBoxBtn;
            sender.broder.BorderBrush = (Brush)(e.NewValue);
        })); 
        //4、按钮点击事件
        public event RoutedEventHandler BtnClick
        {
            add { this.AddHandler(BtnClickEvent, value); }
            remove { this.RemoveHandler(BtnClickEvent, value); }
        }
        public static readonly RoutedEvent BtnClickEvent = EventManager.RegisterRoutedEvent("BtnClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LabelTextBoxBtn));

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RoutedEventArgs args = new RoutedEventArgs(LabelTextBoxBtn.BtnClickEvent, this);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
            }
        }
        //5、内容输入事件
        public event RoutedEventHandler TxtKeyDown
        {
            add { this.AddHandler(TxtKeyDownEvent, value); }
            remove { this.RemoveHandler(TxtKeyDownEvent, value); }
        }
        public static readonly RoutedEvent TxtKeyDownEvent = EventManager.RegisterRoutedEvent("TxtKeyDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(LabelTextBoxBtn));

        public event PropertyChangedEventHandler PropertyChanged;

        //输入框内容变化事件
        private void TBox_text_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Text = (sender as TextBox).Text;

        }
        //按键按下事件
        private void TBox_text_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                this.KeyEventArgs = e;
                RoutedEventArgs args = new RoutedEventArgs(LabelTextBoxBtn.TxtKeyDownEvent, this);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                MessageBox.Show("" + e1.Message);
            }
        }
        //获取到焦点后后显示按钮
        private void Grid_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Btn.Visibility.Equals(Visibility.Collapsed))
                {
                    this.TBox_text.Width -= this.Btn.Width;
                }
                this.Btn.Visibility = Visibility.Visible;
            }
            catch (Exception e1) { }
        }
        //失去焦点就隐藏按钮
        private void Grid_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsAlwaysShowBtn) return;
                if (this.Btn.Visibility.Equals(Visibility.Visible))
                {
                    this.TBox_text.Width += this.Btn.Width;
                }
                this.Btn.Visibility = Visibility.Collapsed;
            }
            catch (Exception e1) { }
        }
    }
}
